﻿namespace WMS6.Web.Common
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;
    using WMS6.Web;

    public class JQUpLoad : BasePage
    {
        private void AssignFunctionResponse(string m_AssignFunctionData)
        {
            base.Response.Clear();
            base.Response.StatusCode = 200;
            base.Response.StatusDescription = "OK";
            base.Response.ContentType = "text/html";
            base.Response.Write(m_AssignFunctionData);
            base.Response.Flush();
            base.Response.End();
        }

        public bool MakeThumbnail(Stream stream, string thumbnailPath, int width, int height, string mode)
        {
            Image originalImage = Image.FromStream(stream);
            int towidth = width;
            int toheight = height;
            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;
            string CS40001 = mode;
            if ((CS40001 != null) && (CS40001 != "HW"))
            {
                if (!(CS40001 == "W"))
                {
                    if (CS40001 == "H")
                    {
                        towidth = (originalImage.Width * height) / originalImage.Height;
                    }
                    else if (CS40001 == "Cut")
                    {
                        if ((((double) originalImage.Width) / ((double) originalImage.Height)) > (((double) towidth) / ((double) toheight)))
                        {
                            oh = originalImage.Height;
                            ow = (originalImage.Height * towidth) / toheight;
                            y = 0;
                            x = (originalImage.Width - ow) / 2;
                        }
                        else
                        {
                            ow = originalImage.Width;
                            oh = (originalImage.Width * height) / towidth;
                            x = 0;
                            y = (originalImage.Height - oh) / 2;
                        }
                    }
                }
                else
                {
                    toheight = (originalImage.Height * width) / originalImage.Width;
                }
            }
            Image bitmap = new Bitmap(towidth, toheight);
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
            try
            {
                MemoryStream temp = new MemoryStream();
                bitmap.Save(temp, ImageFormat.Jpeg);
                using (Stream localFile = new FileStream(thumbnailPath, FileMode.OpenOrCreate))
                {
                    localFile.Write(temp.ToArray(), 0, (int) temp.Length);
                }
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string dir = HttpContext.Current.Request.PhysicalApplicationPath;
            HttpPostedFile userPostedFile = base.Request.Files["FileData"];
            if (userPostedFile.ContentLength > 0)
            {
                string savePath = string.Empty;
                string fileName = string.Empty;
                if (base.Request.QueryString["Type"] == "REPORT")
                {
                    fileName = userPostedFile.FileName;
                    savePath = dir + "UDFReport/" + base.UserInfo.CurrentWH + "/";
                    if (!Directory.Exists(savePath))
                    {
                        Directory.CreateDirectory(savePath);
                    }
                    savePath = savePath + fileName;
                    userPostedFile.SaveAs(savePath);
                    this.AssignFunctionResponse(fileName);
                }
                else
                {
                    fileName = DateTime.Now.ToString("yyyymmddhhss") + ".JPG";
                    savePath = dir + "Upload/OwnerLogo/" + fileName;
                    if (File.Exists(savePath))
                    {
                        File.Delete(savePath);
                    }
                    if (this.MakeThumbnail(userPostedFile.InputStream, savePath, 210, 60, "HW"))
                    {
                        this.AssignFunctionResponse(fileName);
                    }
                    else
                    {
                        this.AssignFunctionResponse("");
                    }
                }
            }
        }
    }
}

